package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class Fragment4 extends Fragment {

    private RecyclerView recyclerView;
    private List<Song> musicList;

    private MusicAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_layout4, container, false);

        recyclerView = view.findViewById(R.id.recyclerViewMusic);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        // 初始化音乐列表，添加 Song 对象
        musicList = new ArrayList<>();
        musicList.add(new Song("普通朋友", R.raw.normalfriend, R.drawable.normalfriend));  // 歌曲名，资源ID，封面图标ID
        musicList.add(new Song("晴天", R.raw.normalfriend, R.drawable.jchou));
        musicList.add(new Song("富士山下", R.raw.fushishanxia, R.drawable.eson));
        musicList.add(new Song("开始懂了", R.raw.beginunderstand, R.drawable.beginunderstand));
        musicList.add(new Song("富士山下", R.raw.fushishanxia, R.drawable.eson));
        musicList.add(new Song("普通朋友", R.raw.normalfriend, R.drawable.normalfriend));
        musicList.add(new Song("富士山下", R.raw.fushishanxia, R.drawable.eson));
        musicList.add(new Song("晴天", R.raw.normalfriend, R.drawable.jchou));
        musicList.add(new Song("开始懂了", R.raw.beginunderstand, R.drawable.beginunderstand));

        // 创建音乐适配器
        adapter = new MusicAdapter(musicList, song -> {
            Log.d("Fragment4", "Song selected: " + song.getTitle());  // 日志记录歌曲选择
            // 启动服务播放音乐
            Intent intent = new Intent(getContext(), MusicService.class);
            intent.putExtra("track_res_id", song.getResId());  // 传递歌曲资源ID
            Log.d("Fragment4", "Starting service to play music with track resId: " + song.getResId());  // 日志记录服务启动
            getContext().startService(intent);  // 启动服务播放音乐
        });

        recyclerView.setAdapter(adapter);

        return view;
    }
}
